package com.xxkk.spring_boot_web.sdk.KBZ.utils;

import okhttp3.*;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class OkHttpUtil {

    private static final String TAG = "OkHttpUtil";

    private static final OkHttpClient sHttpClient = new OkHttpClient.Builder()
            .readTimeout(1, TimeUnit.MINUTES)
            .build();

    public static String request(String url) throws IOException {
        Request request = new Request.Builder()
                .url(url)
                .build();
        try (Response response = sHttpClient.newCall(request).execute()) {
            return response.body().string();
        }
    }
    public static String encryption(String str) {
        return RSAUtil.encrypt(str, "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEArvCjjTiXtN++gUs3HHBTlqV6ZslJ/JXOyBO93lI0y5QjcMpe27xfljy6snHkFEMOibz+XibNqgLFxhCFXrcl9ac0naphGhnabWDvlg4W/Wvg3IOm5s+PUNXl0jyr1yahVvSHOvQlU9+HzbvLlUtXGLS+EGUcXuu3tQB3MUd+XgDjRwLReeGw0YNDOwqQ8Wt7udaXE/+hiM3xKD7pQJ+ACGX1YtLh+OsV2RVpA9Hqbz5yaxpQScM+HO8sS/Q6sEG0Bto6ngVBtSAs5vtA5gBMUeeQgtg8MDnr1Gq/sOo5k27YsdEiJh4IO5yrEAY9fgX0HJ15MXyUaPjfrfRFs9unoQIDAQAB");
    }

    public static String getServerPublicKey() {
        return "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAh/3LI5TjENXjwdr85cK9wZW+MTBLd8AlZoRtMU4+8E/Yj6u0vhNtGvumR20Q3d82/N4+kva8suqEQjy4QeOjFvRJywZxOkiNl+snv2TWZLQ2MVD5MtqVY0LxmAT7iO3iDEtiBhBhfYQA+uGoelceKAMD3V+6FXNpjh75vCHskR+fZJBXtEmvDMmwB5co/j5jsmHdoPSCKunZbGxXsiUJPMxqDtrF2Lt8WVqSctr46sdQB13qJ3IdrtWb4hkgZlcVrn898t+Wxa753yjKEfzZ0GzNV+Ih6IWN+n4q+Kkv6DIFtZHnRMqfnURF/zOFJif/yxW8vMNdIMtRBJa1F/8mkQIDAQAB";
    }

    public static String getData(String bodyString, boolean isNew) {
        long currentTimeMillis = System.currentTimeMillis();

        String key = AESUtil.genarateRandomKey();   //加密的key
        String iv = RandomUtil.getRandom(16);    //iv

        String encryptKey = RSAUtil.encrypt(key, getServerPublicKey());
        String encryptIv = RSAUtil.encrypt(iv, getServerPublicKey());

        String sign = HMacSha256Util.hmacSHA256(currentTimeMillis + iv + bodyString, key);  //生成header的sign

        String encryptContent = AESUtil.encrypt(bodyString, key, iv);  //body加密
        MediaType mediaType = MediaType.parse("application/json; charset=utf-8");
        RequestBody requestBody = RequestBody.create(mediaType, encryptContent);
        Request.Builder builder = new Request.Builder();
        builder.url("https://app.kbzpay.com:9002/api/interface/version1.1/customer");
        builder.method("POST", requestBody);
        if(isNew){
            builder.addHeader("MessageType", "NEW");
        }
        builder.addHeader("Authorization", encryptKey);
        builder.addHeader("IvKey", encryptIv);
        builder.addHeader("Sign", sign);
        builder.addHeader("Timestamp", currentTimeMillis + "");
        builder.addHeader("Content-Type", "application/json; charset=utf-8");
      //  builder.addHeader("Content-Length", "856");
        builder.addHeader("Host", "app.kbzpay.com:9002");
        builder.addHeader("Connection", "Keep-Alive");
        builder.addHeader("Accept-Encoding", "gzip");
        builder.addHeader("User-Agent", "okhttp/4.10.0");
        Request request = builder.build();
        try {
            Response response = sHttpClient.newCall(request).execute();
            String respString = response.body().string();
            String json = AESUtil.decrypt(respString, key, iv);  //返回结果解密   参数错误放回的结果是文明的
            return json;
        } catch (IOException e) {
            e.printStackTrace();
            return "";
        }
    }





}
